var ticker = 2222;
var chatTicker = 2222;
var command_str = "";

var CARD_IMAGE_PATH  = {}; // New object
    //<img src="" draggable="true" style="cursor: move;">
CARD_IMAGE_PATH['001'] = "images/game/cards/1.png";
CARD_IMAGE_PATH['002'] = "images/game/card_ch2.png";
CARD_IMAGE_PATH['003'] = "images/game/card_ch3.png";

var LastParsedState; //Save Last Parsed State - For Restarting Move
var IsBoardPainted = false;  // tell if PlayerBoard is full already from paint_state()
var ISMYTURN = false;
//Save the Last card the user put in board, to restore it on unplace
var LastCardElement;    
var LastCardID;

// For Click Here Click There
var ClickHere_CardID = null;
var ClickHere_LastElement = null;

// to show Winner\Loser screen
// 0-no , 1(WINNER_STATE)-win , 2(LOSER_STATE)-lose
var IsGameEnded = 0;    
var WINNER_STATE = 1;
var LOSER_STATE =2;

//////////////////////////////////////////
var PlayerBoard = [];
for (var i=0;i<4;i++) {
    PlayerBoard[i] = [];
}
var OpponentBoard = [];
for (var i=0;i<4;i++) {
    OpponentBoard[i] = [];
}
var PlayerTowers = [];
var OpponentTowers= [];
var PlayerCards;

// Error massage
var commandLineDeckFull = "Deck is full";
var commandLineDrawNotAlone = "Drawing a card can only come with an attacking move";
var commandLineSurrenderNotAlone = "Surrender can only come alone";
var commandLineINotInBoard = "Out of boundries";
var commandLineJNotInBoard = "Out of boundries";
var commandLinePlaceSoldierNotAlone = "Placing a soldier while attacking with another is not possible";
var commandLineCellFull = "Can not place soldier in a full cell";
var commandLineNoCard = "Deck is empty";
var commandLineNoCardInCell = "Cell is empty";
var commandLineAttackNotAlone = "Attacking move should be executed alone";
var commandLineEmptyCommand = "Please make a move";


// Where User choose to put Soldier
// Set by GUI (Drag)
var PlaceSoldierType = {
    'Card': false,      // card index 1-5
    'i':-1,
    'j':-1
};

// Where User choose to Attack
// Set by GUI (Click)
var AttackType = {
    'i':-1,
    'j':-1
};

// store GUI command
var NextCommand = {
    'ErrorMessage': "",     // if CheckCommand() returns false --> ErrMSG 
    'DrawCard': false,      // True \ False
    'PlaceSoldier': false,  // (card index),i,j (PlaceSoldierType)
    'Attack': false,        // i,j              (AttackType)
    'Surrender': false,     // True \ False
    //Initialize Struct (for each movement)
    'Init': function() {
        this.DrawCard = false;
        this.PlaceSoldier = false;
        this.Attack = false;
        this.Surrender = false;
    },
    // Check Command (used before submitting GUI command)

    'CheckCommand': function() {
        if(this.DrawCard != false) {
            if(PlayerCards[PlayerCards.length - 1]['IsEmpty'] != true) {
                this.ErrorMessage = commandLineDeckFull;
                return false;
            }
            if( (this.Surrender != false) || (this.PlaceSoldier != false) ) {
                this.ErrorMessage = commandLineDrawNotAlone;
                return false;
            }
        }
        if( (this.Surrender != false) && ( (this.DrawCard != false) || (this.PlaceSoldier != false) || (this.Attack != false) )) {
            this.ErrorMessage = commandLineSurrenderNotAlone;
            return false;
        }
        if( this.PlaceSoldier != false) {
            if( (PlaceSoldierType.i >= ROWS) || (PlaceSoldierType.i < 0) ) {
                this.ErrorMessage = commandLineINotInBoard;
                return false;
            }
            if( (PlaceSoldierType.j >= COLS) || (PlaceSoldierType.j < 0) ) {
                this.ErrorMessage = commandLineJNotInBoard;
                return false;
            }
            if( (this.DrawCard != false) || (this.Surrender != false) || (this.Attack != false) ) {
                this.ErrorMessage = commandLinePlaceSoldierNotAlone;
                return false;
            }
            if(PlayerBoard[PlaceSoldierType.i][PlaceSoldierType.j]['IsEmpty'] != true) {
                this.ErrorMessage = commandLineCellFull;
                return false;
            }
        }
        if(this.Attack != false) {
            if( (AttackType.i >= ROWS) || (AttackType.i < 0) ) {
                this.ErrorMessage = commandLineINotInBoard;
                return false;
            }
            if( (AttackType.j >= COLS) || (AttackType.j < 0) ) {
                this.ErrorMessage = commandLineJNotInBoard;
                return false;
            }
            if(PlayerBoard[AttackType.i][AttackType.j]['IsEmpty'] != false) {
                this.ErrorMessage = commandLineNoCardInCell;
                return false;
            }
            if( (this.Surrender != false) || (this.PlaceSoldier != false) ) {
                this.ErrorMessage = commandLineAttackNotAlone;
                return false;
            }
        }
        if( (this.Attack == false) && (this.Surrender == false) && (this.PlaceSoldier == false) && (this.DrawCard == false) ) {
            this.ErrorMessage = commandLineEmptyCommand;
            return false;
        }
        return true;
    },
    //
    'GenerateCommandString': function() {
        if ( this.CheckCommand() ) {
            var tmpStr = "";
            
            if(this.DrawCard != false) {
                tmpStr = COM_DRAW_CARD;
                if(this.Attack != false) {
                    tmpStr = tmpStr + "|" + COM_ATTACK + " " + AttackType.i + " " + AttackType.j;
                }
            }
            else if(this.Attack != false) {
                tmpStr = COM_ATTACK + " " + AttackType.i + " " + AttackType.j;;
            }
            else if(this.PlaceSoldier != false) {
                var card_id = PlaceSoldierType.Card;
                card_id = 1 + parseInt(card_id) ;
                tmpStr = COM_PLACE_CARD + " " + card_id  + " " + PlaceSoldierType.i + " " + PlaceSoldierType.j;
            }
            else if(this.Surrender != false) {
                tmpStr = COM_FORFIET;
            }
            return tmpStr;
        }
        return false;
    }    
};


/****** Animation ******/
var HIT_COLOR    = '#ffa6a6';
var ATTACK_COLOR       = '#a6ffac';


